class TreeNode {
    int val;
    public TreeNode left;
    public TreeNode right;
    public TreeNode(int val) {
        this.val = val;
    }
}
public class Solution {
    public int rob(int[] nums) {
        int n = nums.length;
        if(n == 1) return nums[0];//处理边界情况
        return Math.max(rob1(nums, 0, n - 2), rob1(nums, 1, n - 1));
    }
    private int rob1(int[] nums, int left, int right) {
        //1.创建dp表
        //2.初始化
        //3.填表
        //4.返回值
        int n = nums.length;
        int[] f = new int[n];
        int[] g = new int[n];

        f[left] = nums[left];
        for(int i = left + 1; i <= right; i++) {
            f[i] = g[i-1] + nums[i];
            g[i] = Math.max(g[i-1], f[i-1]);
        }
        return Math.max(f[right], g[right]);
    }
    public long prev = Long.MIN_VALUE;
    public boolean isValidBST(TreeNode root) {
        if(root == null) return true;
        boolean leftTree = isValidBST(root.left);

        //剪枝
        if(!leftTree) return false;

        boolean cur = true;
        if(root.val <= prev) cur = false;

        //剪枝
        if(!cur) return false;
        
        prev = root.val;
        boolean rightTree = isValidBST(root.right);
        return leftTree && rightTree && cur;
    }
}